import log from '@dfeidao/atom-nodejs/logger/log';
import { IncomingHttpHeaders } from 'http';
import query from '@dfeidao/atom-nodejs/db/query';
import { systemid, productid } from '../../atom/config';
import count from '@dfeidao/atom-nodejs/db/count';
interface Message {
	filter: {
		page_no: number,
		hidden_source: string,
		hidden_name: string,
	}
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string;
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}
// 表名	hazard_source				
// _id	id	string	50	ⅹ	√
// productid	产品ID	string	50	√	ⅹ
// hidden_source	隐患源编号	string	32	√	ⅹ
// duty_department_no	责任部门编号	string	50	√	ⅹ
// duty_department	责任部门名称	string	50	√	ⅹ
// responsible	责任人名称	string	50	√	ⅹ
// responsible_task_no	任务责任人编号	string	50	√	ⅹ
// hidden_code	隐患源代码	string	32	√	ⅹ
// hidden_name	隐患源名称	string	32	√	ⅹ
// control_level	管控级别	string	32	√	ⅹ
// control_grade	管控层级	string	32	√	ⅹ
// assess_level	评价级别	string	32	√	ⅹ
// engineer_skill	工程技术	string	128	√	ⅹ
// manage_measure	管理措施	string	128	√	ⅹ
// train_education	培训教育	string	128	√	ⅹ
//                7.4.3.2将【7.4.3.1】中的数据展示到弹窗3的列表：
//                               “隐患源编号”,     对应“隐患源编号”
//                               “管控级别”，      对应”管控级别“
//                               “工程技术”，      对应“工程技术”
//                               “负责部门名称”，对应“负责部门”
//                               “责任人名称”，   对应“负责人”      

export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	log('Service begin path:e:\Feidao\kh\src\risk-point\zj-000008\s001,action_id:' + action_id);
	const { prepare, exec } = query(action_id, session_id, systemid);
	prepare('hazard_source', ['hidden_source', 'hidden_name', 'control_level', 'engineer_skill', 'duty_department', 'responsible'], { productid, hidden_source: { $like: '%' + message.filter.hidden_source + '%' }, hidden_name: { $like: '%' + message.filter.hidden_name + '%' } }, 5, message.filter.page_no, [], []);
	const [data] = await exec();

	const { prepare: p, exec: e } = count(action_id, session_id, systemid);
	p('hazard_source', { productid, hidden_source: { $like: '%' + message.filter.hidden_source + '%' }, hidden_name: { $like: '%' + message.filter.hidden_name + '%' } });
	const [n] = await e();

	log('Service end path:e:\Feidao\kh\src\risk-point\zj-000008\s001,action_id:' + action_id);
	return {
		data: { data, n }
	};
}
